//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using SoapTrace.Core.Communication;
using System;
using System.IO;

namespace SoapTrace.Core.Diagnostics
{
	/// <summary>
	/// Logs soap messages to single folder.
	/// </summary>
	public sealed class SoapMessageLogger : ISoapMessageLogger
	{
		/// <summary>
		/// Logging enabled status flag.
		/// </summary>
		private bool m_Enabled;
		
		/// <summary>
		/// The log file folder.
		/// </summary>
		private string m_LogFileFolder = String.Empty;
		
		/// <summary>
		/// Creates a new instance of the 
		/// <see cref="SoapMessageLogger"/>.
		/// </summary>
		public SoapMessageLogger( )
		{
		}
		
		/// <summary>
		/// Turns logging on or off.
		/// </summary>
		public bool Enabled
		{
			get
			{
				return m_Enabled;
			}
			
			set
			{
				m_Enabled = value;
			}
		}
		
		/// <summary>
		/// Gets or sets the log file folder.
		/// </summary>
		public string Folder
		{
			get
			{
				return m_LogFileFolder;
			}
			
			set
			{
				m_LogFileFolder = value;
			}
		}
		
		/// <summary>
		/// Called when a soap request and response have arrived.
		/// </summary>
		/// <param name="serverAddressFilter">The remote server's IP address
		/// or host name used when filtering.</param>
		/// <param name="serverFilterPort">The remote server's port
		/// used when filtering.
		/// </param>
		/// <param name="requestAndResponse">The soap request and 
		/// response.</param>
		public void Log( string serverAddressFilter, 
			int serverFilterPort, 
			SoapRequestAndResponse requestAndResponse )
		{
			if( m_Enabled )
			{
				try
				{
					CreateLogFolder( );
					WriteLogFile( serverAddressFilter, serverFilterPort, requestAndResponse );
				}
				catch( IOException Ex )
				{
					Logger.LogDebug( String.Concat( "Unable to log soap request/response. ", Ex.ToString( ) ) );
				}
			}
		}
		
		/// <summary>
		/// Creates an xml file for the request and response pair.
		/// </summary>
		/// <param name="serverAddressFilter">The remote server's IP address
		/// or host name used when filtering.</param>
		/// <param name="serverPortFilter">The remote server's port
		/// used when filtering.
		/// </param>
		/// <param name="requestAndResponse">The soap request and 
		/// response.</param>
		private void WriteLogFile( string serverAddressFilter, 
			int serverPortFilter, 
			SoapRequestAndResponse requestAndResponse )
		{
			string Filename = String.Concat( DateTime.Now.ToString( "yyyy-MM-dd HH_mm_ss_fff" ), ".xml" );
			
			SoapMessageLogFile.Save( Path.Combine( m_LogFileFolder, Filename ),
				serverAddressFilter,
			    serverPortFilter,
				requestAndResponse );
		}
		
		/// <summary>
		/// Creates the log file folder if it does not exist.
		/// </summary>
		private void CreateLogFolder( )
		{
			if( false == Directory.Exists( m_LogFileFolder ) )
			{
				Directory.CreateDirectory( m_LogFileFolder );
			}
		}
	}
}
